<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="utf-8" />
<link rel="icon" href="favicon.png" />
<title>Test drive ▲ Prism</title>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="themes/prism.css" data-noprefix />
<style>
#theme {
	margin-right: -9em;
}

textarea {
	width: 100%;
	height: 10em;
	padding: 1em;
	box-sizing: border-box;
	margin: .5em 0;
	background: #f5f2f0;
	color: black;
	text-shadow: 0 1px white;
	tab-size: 4;
	font: 100% Consolas, Monaco, monospace;
	white-space: pre;
	word-wrap: normal;
}

#language {
	column-count: 4;
}

	#language label {
		display: block;
		padding: .2em;
	}

	#language label[data-id="javascript"] {
		border-bottom: 1px solid #aaa;
		padding-bottom: 1em;
		margin-bottom: 1em;
	}

	#language input {
		margin-right: 1em;
	}

	#language strong {
		display: block;
		column-span: all;
	}

</style>
<script src="prefixfree.min.js"></script>

<script>var _gaq = [['_setAccount', 'UA-33746269-1'], ['_trackPageview']];</script>
<script src="https://www.google-analytics.com/ga.js" async></script>
</head>
<body>

<header>
	<div class="intro" data-src="templates/header-main.html" data-type="text/html"></div>

	<h2>Test drive</h2>
	<p>Take Prism for a spin!</p>
</header>

<section>
	<form>
		<p>
			<textarea>&lt;p class="hey">Type some code here&lt;/p></textarea>
		</p>

		<p>Result:</p>
		<pre><code></code></pre>

		<p id="language">
			<strong>Language:</strong>
		</p>
	</form>
</section>

<footer data-src="templates/footer.html" data-type="text/html"></footer>

<script src="prism.js"></script>
<script src="utopia.js"></script>
<script src="components.js"></script>
<script src="code.js"></script>
<script src="vendor/promise.js"></script>

<script>
(function() {
var form = $('form'), code = $('code', form),
    languages = components.languages,
    highlightCode = function() { Prism.highlightElement(code); };

for (var id in languages) {
	if (id == 'meta') {
		continue;
	}

	var name = languages[id].title || languages[id];

	$u.element.create('label', {
		attributes: {
			'data-id': id
		},
		contents: [
			{
				tag: 'input',
				properties: {
					type: 'radio',
					name: 'language',
					value: id,
					onclick: function () {
						var lang = this.value;
						code.className = 'language-' + lang;
						code.textContent = code.textContent;

						// loadLanguage() returns a promise, so we use highlightCode()
						// as resolve callback. The promise will be immediately
						// resolved if there is nothing to load.
						loadLanguage(lang).then(highlightCode);
					}
				}
			}, name
		],
		inside: '#language'
	});
}


/**
 * Loads a language, including all dependencies
 *
 * @param {string} lang the language to load
 * @type {Promise} the promise which resolves as soon as everything is loaded
 */
function loadLanguage (lang)
{
	// at first we need to fetch all dependencies for the main language
	// Note: we need to do this, even if the main language already is loaded (just to be sure..)
	//
	// We load an array of all dependencies and call recursively this function on each entry
	//
	// dependencies is now an (possibly empty) array of loading-promises
	var dependencies = getDependenciesOfLanguage(lang).map(loadLanguage);

	// We create a promise, which will resolve, as soon as all dependencies are loaded.
	// They need to be fully loaded because the main language may extend them.
	return Promise.all(dependencies)
		.then(function () {

			// If the main language itself isn't already loaded, load it now
			// and return the newly created promise (we chain the promises).
			// If the language is already loaded, just do nothing - the next .then()
			// will immediately be called
			if (!Prism.languages[lang]) {
				return new Promise(function (resolve) {
					$u.script('components/prism-' + lang + '.js', resolve);
				});
			}
		});
}


/**
 * Returns all dependencies (as identifiers) of a specific language
 *
 * @param {string} lang
 * @returns {Array.<string>} the list of dependencies. Empty if the language has none.
 */
function getDependenciesOfLanguage (lang)
{
	if (!components.languages[lang] || !components.languages[lang].require)
	{
		return [];
	}

	return ($u.type(components.languages[lang].require) === "array")
		? components.languages[lang].require
		: [components.languages[lang].require];
}


var radios = $$('input[name=language]');
radios[0].checked = true;
radios[0].onclick();

var textarea = $('textarea', form);

(textarea.oninput = function() {
	code.textContent = this.value || '';
	highlightCode();
}).call(textarea);

})();

</script>

</body>
</html>
